/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package iplayer;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javafx.async.RunnableFuture;
import javax.imageio.ImageIO;

/**
 *
 * @author stephengalbraith
 */
public class AsyncGetIplayerStreamInfo  implements RunnableFuture{
    private Program program;
    private String get_iplayer;

    public AsyncGetIplayerStreamInfo(Program program,String get_iplayer)
    {
        this.program = program;
        this.get_iplayer=get_iplayer;
        
    }
    public void run() {
        try {
            System.out.println("./resources/get_iplayer"+" --info "+" --streaminfo " +"--types"+" --fields=pid "+this.program.pid);
            final Process process = Runtime.getRuntime().exec(new String[]{this.get_iplayer,"--streaminfo","--modes",this.program.defaultMode,"--fields=pid",this.program.pid });
            final InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
            BufferedReader buf = new BufferedReader(inputStreamReader);
            String line = "";
            process.waitFor();

            System.out.println(process.exitValue());
            boolean isIphone=false;
            while (line != null) {
                line = buf.readLine();
                System.out.println("Read line:" + line);
                if (line != null) {
                    if (line.startsWith("streamurl:"))
                    {
                        if (null==program.streamURL && line.contains("default:") )
                        {
                            System.out.println("******"+line.substring(19).trim()+"****");
                        program.streamURL=line.substring(19).trim();
                        }
                    }


                }

            }
            
            System.out.println("done");
            buf.close();
            inputStreamReader.close();
            process.getInputStream().close();
        } catch (Exception e) {
            // never
            e.printStackTrace();
        }
    }
}
